package com.edvinli.service.impl;

import com.edvinli.entity.PageBean;
import com.edvinli.entity.Student;
import com.edvinli.entity.StudentQueryParam;
import com.edvinli.mapper.StudentMapper;
import com.edvinli.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询
     * @param param
     * @return
     */
    @Override
    public PageBean page(StudentQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Student> list = studentMapper.page(param);
        PageInfo<Student> pageInfo = new PageInfo<>(list);

        return new PageBean(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增学生
     * @param student
     */
    @Override
    public void create(Student student) {
        //补充基础信息
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());

        //调用mapper
        studentMapper.insert(student);
    }

    /**
     * 通过id查询学员信息回显
     * @param id
     * @return
     */
    @Override
    public Student getById(Integer id) {

        return studentMapper.getById(id);
    }

    /**
     * 修改学员信息
     * @param student
     */
    @Override
    public void update(Student student) {
        //设置基础的属性
        student.setUpdateTime(LocalDateTime.now());

        //调用修改方法
        studentMapper.update(student);
    }

    /**
     * 根据id批量删除学员
     * @param ids
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    /**
     * 违纪记录
     * @param id
     * @param score
     */
    @Override
    public void violate(Integer id, Integer score) {
        studentMapper.putViolation(id, score);
    }
}
